import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Test {
    public static void main(String[] args) throws SQLException {
        //创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://localhost:3306/java110?characterEncoding=utf-8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        //和数据库建立连接
        Connection connection = dataSource.getConnection();

        //构造一个sql
        String sql = "insert into commodity values('张三',1,1,'')";
        //把需要 String sql 转成 语句对象
        PreparedStatement statement = connection.prepareStatement(sql);

        //把构造好的sql发送给服务器
        int n = statement.executeUpdate();
        System.out.println("n = " + n);

        //释放资源
        statement.close();
        connection.close();
    }
}
